package com.ruoyi.waterDistribution.controller;

import java.io.InputStream;
import java.math.BigDecimal;
import java.util.*;
import java.io.IOException;
import java.util.stream.Collectors;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.mysql.cj.log.LogFactory;
import com.ruoyi.common.core.domain.business.waterDistribution.WiOrgplanM;
import com.ruoyi.common.core.utils.bean.BeanMapUtils;
import com.ruoyi.common.core.utils.business.BusinessUtils;
import com.ruoyi.waterDistribution.service.IWiOrgplanMService;
import com.ruoyi.waterDistribution.vo.WiLandBVo;
import com.ruoyi.waterDistribution.vo.YearPlanExeclVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 月机构限额水量及计划水量计算Controller
 *
 * @author ps
 * @date 2023-02-16
 */
@RestController
@RequestMapping("/WiOrgplanM")
public class WiOrgplanMController extends BaseController {
    @Autowired
    private IWiOrgplanMService wiOrgplanMService;

    private static final Logger log = LoggerFactory.getLogger(WiOrgplanMController.class);

    /**
     * 导入上一年数据
     */
    @GetMapping("/importLastYearPlan")
    public AjaxResult importLastYearPlan(String year, String orgCd) {
        log.info("导入上一年数据 入参 : " + year + "=" + orgCd);
        List<WiOrgplanM> wiOrgplanMList = wiOrgplanMService.importLastYearPlan(year, orgCd);
        return AjaxResult.success(wiOrgplanMList);
    }


    /**
     * 配水分析 -- 显示各级水管机构计划用水和实测水量的对比
     */
    @GetMapping("/orgVolAnalys")
    public AjaxResult orgVolAnalys(String year, String orgCd) {
        log.info("年度配水分析 入参 : " + year + "=" + orgCd);
        HashMap<String, Object> res = wiOrgplanMService.orgVolAnalys(year, orgCd);
        return AjaxResult.success(res);
    }

    /**
     * 年度用水计划
     */
    @GetMapping("/getOrgPlanYear")
    public AjaxResult getOrgPlanYear(String year, String orgCd) {
        log.info("年度用水计划 入参 : " + year + "=" + orgCd);
        ArrayList<Object> resList = wiOrgplanMService.getOrgPlanYear(year, orgCd);
        return AjaxResult.success(resList);
    }

    /**
     * 汇总各级机构的逐月旬用水
     */
    @GetMapping("/getOrgPlanByPlanYmAndOrgCd")
    public TableDataInfo getOrgPlanByPlanYmAndOrgCd(String year, String orgCd) {
        log.info("汇总各级机构的逐月旬用水 : " + year + "=" + orgCd);
        ArrayList<Object> wiOrgplanMList = wiOrgplanMService.getOrgPlanByPlanYmAndOrgCd(year, orgCd);
        return getDataTable(wiOrgplanMList);
    }

    /**
     * 根据机构编码获取该机构下属所有地块
     *
     * @param orgCd 机构代码
     * @return
     */
    @GetMapping("/getWiLandBListByOrgCd")
    public TableDataInfo getWiLandBListByOrgCd(String year, String orgCd) {
        log.info("getWiLandBListByOrgCd入参 : " + orgCd);
        List<WiLandBVo> list = wiOrgplanMService.getWiLandBListByOrgCd(year, orgCd);
        return getDataTable(list);
    }

    /**
     * 汇总该机构当年各月旬计划
     */
    @GetMapping("/calcTenPlan")
    public AjaxResult getTenPlan(String orgCd, String year) throws Exception {
        log.info("calcTenPlan 入参 : " + orgCd + "==" + year);
        return toAjax(wiOrgplanMService.setTenPlan(orgCd, year));
    }

    /**
     * 查询月机构限额水量及计划水量计算列表
     */
    @RequiresPermissions("distribute:WiOrgplanM:list")
    @GetMapping("/list")
    public TableDataInfo list(WiOrgplanM wiOrgplanM) {
//        startPage();
        List<WiOrgplanM> list = wiOrgplanMService.selectWiOrgplanMList(wiOrgplanM);
        return getDataTable(list);
    }

    /**
     * 导出月机构限额水量及计划水量计算列表
     */
    @RequiresPermissions("distribute:WiOrgplanM:export")
    @Log(title = "月机构限额水量及计划水量计算", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, String year, String orgCd) throws Exception {
        ArrayList<Object> resList = wiOrgplanMService.getOrgPlanYear(year, orgCd);
        List<YearPlanExeclVo> list = new ArrayList<>();
        for (Object obj : resList) {
            YearPlanExeclVo yearPlanExeclVo = BeanMapUtils.mapToBean((Map<String, Object>) obj, YearPlanExeclVo.class);
            list.add(yearPlanExeclVo);
        }
        ExcelUtil<YearPlanExeclVo> util = new ExcelUtil<YearPlanExeclVo>(YearPlanExeclVo.class);
        util.exportExcel(response, list, "月机构限额水量及计划水量计算数据");
    }

    //导出用水过程表
    @PostMapping("/export2")
    public void export2(HttpServletResponse response, String year, String orgCd, String tableTitle) throws Exception {
        log.info("逐月用水过程导出");
        InputStream inputStream = null;
        inputStream = this.getClass().getClassLoader().getResourceAsStream("static/file/用水计划过程表.xlsx");
        ArrayList<Object> wiOrgplanMList = wiOrgplanMService.getOrgPlanByPlanYmAndOrgCd(year, orgCd);
        Map<String, String> map = new HashMap<>();
        map.put("excelTitle", tableTitle);
        map.put("year", year);
        BigDecimal sum1 = new BigDecimal("0");
        BigDecimal sum2 = new BigDecimal("0");
        BigDecimal sum3 = new BigDecimal("0");
        BigDecimal yearSum = new BigDecimal("0");
        for (int i = 0; i < wiOrgplanMList.size(); i++) {
            ArrayList<String> value = (ArrayList<String>) wiOrgplanMList.get(i);
            map.put("value" + (i + 1), value.get(4));
            if (value.get(1).equals("春灌") && value.get(3).equals("月总量")) {
                sum1 = sum1.add(new BigDecimal(value.get(4)));
            }
            if (value.get(1).equals("夏灌") && value.get(3).equals("月总量")) {
                sum2 = sum2.add(new BigDecimal(value.get(4)));
            }
            if (value.get(1).equals("冬灌") && value.get(3).equals("月总量")) {
                sum3 = sum3.add(new BigDecimal(value.get(4)));
            }
            if (value.get(3).equals("月总量")) {
                yearSum = yearSum.add(new BigDecimal(value.get(4)));
            }
            map.put("sum1", sum1 + "");
            map.put("sum2", sum2 + "");
            map.put("sum3", sum3 + "");
            map.put("yearSum", yearSum + "");
        }
        ServletOutputStream out = response.getOutputStream();
        ExcelWriter excelWriter = EasyExcel.write(out).withTemplate(inputStream).build();
        WriteSheet writeSheet = EasyExcel.writerSheet().build();
        excelWriter.fill(map, writeSheet);
        excelWriter.finish();
    }

    /**
     * 获取月机构限额水量及计划水量计算详细信息
     */
    @RequiresPermissions("distribute:WiOrgplanM:query")
    @GetMapping(value = "/{orgCd}")
    public AjaxResult getInfo(@PathVariable("orgCd") String orgCd) {
        return success(wiOrgplanMService.selectWiOrgplanMByOrgCd(orgCd));
    }

    /**
     * 新增月机构限额水量及计划水量计算
     */
    @RequiresPermissions("distribute:WiOrgplanM:add")
    @Log(title = "月机构限额水量及计划水量计算", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WiOrgplanM wiOrgplanM) {
        return toAjax(wiOrgplanMService.insertWiOrgplanM(wiOrgplanM));
    }

    /**
     * 修改月机构限额水量及计划水量计算
     */
    @RequiresPermissions("distribute:WiOrgplanM:edit")
    @Log(title = "月机构限额水量及计划水量计算", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WiOrgplanM wiOrgplanM) {
        return toAjax(wiOrgplanMService.updateWiOrgplanM(wiOrgplanM));
    }

//    /**
//     * 删除月机构限额水量及计划水量计算 废弃 计划可以重新汇总，不需要单独删除
//     */
//    @RequiresPermissions("distribute:WiOrgplanM:remove")
//    @Log(title = "月机构限额水量及计划水量计算", businessType = BusinessType.DELETE)
//    @DeleteMapping("/{orgCds}")
//    public AjaxResult remove(@PathVariable String[] orgCds) {
//        return toAjax(wiOrgplanMService.deleteWiOrgplanMByOrgCds(orgCds));
//    }
}
